package com.atguigu.champter5.Flink05.transformation.aggregation;

import com.atguigu.beans.WaterSensor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;

public class $2_Sum_Min_Max {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);

        ArrayList<WaterSensor> waterSensors = new ArrayList<>();
        waterSensors.add(new WaterSensor("sensor_1", 1607527992000L, 50));
        waterSensors.add(new WaterSensor("sensor_1", 1607527994000L, 20));
        waterSensors.add(new WaterSensor("sensor_1", 1607527996000L, 60));
        waterSensors.add(new WaterSensor("sensor_2", 1607527993000L, 10));
        waterSensors.add(new WaterSensor("sensor_2", 1607527995000L, 30));

        DataStreamSource<WaterSensor> waterSensorDS = env.fromCollection(waterSensors);

        waterSensorDS
                .keyBy(new KeySelector<WaterSensor, String>() {
                    @Override
                    public String getKey(WaterSensor value) throws Exception {
                        return value.getId();
                    }
                })
                //solt内聚合
                //只修改聚合值,其它是改分区的第一条数据
                .sum("vc")
                //只修改聚合值,其它是改分区的第一条数据
//                .min("vc")
                //取最大字段所在数据的全部
//                .minBy("vc")
//                .max("vc")
//                .maxBy("vc")
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
